{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 14.3 项目实施"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-11T16:24:23.523594Z",
     "start_time": "2018-12-11T16:24:22.869736Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'status': 0,\n",
       " 'message': 'ok',\n",
       " 'total': 400,\n",
       " 'results': [{'name': '后海公园',\n",
       "   'location': {'lat': 39.948473, 'lng': 116.388894},\n",
       "   'address': '北京市西城区松树街1号',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '西城区',\n",
       "   'street_id': '7aea43b721dd95e17d29bda4',\n",
       "   'detail': 1,\n",
       "   'uid': '7aea43b721dd95e17d29bda4',\n",
       "   'detail_info': {'tag': '旅游景点;公园',\n",
       "    'navi_location': {'lng': 116.38864586804, 'lat': 39.947822400829},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=7aea43b721dd95e17d29bda4&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '4.4',\n",
       "    'comment_num': '168',\n",
       "    'children': []}},\n",
       "  {'name': '南海子公园',\n",
       "   'location': {'lat': 39.779482, 'lng': 116.475558},\n",
       "   'address': '黄亦路16号',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '大兴区',\n",
       "   'street_id': 'f1f5cac51d653e8137b06ce4',\n",
       "   'telephone': '(010)69271456',\n",
       "   'detail': 1,\n",
       "   'uid': 'f1f5cac51d653e8137b06ce4',\n",
       "   'detail_info': {'tag': '旅游景点;公园',\n",
       "    'navi_location': {'lng': 116.47869080683, 'lat': 39.774373927351},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=f1f5cac51d653e8137b06ce4&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '3.7',\n",
       "    'comment_num': '34',\n",
       "    'children': []}},\n",
       "  {'name': '通州大运河森林公园',\n",
       "   'location': {'lat': 39.8824, 'lng': 116.748887},\n",
       "   'address': '北京市通州区宋梁路南段',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '通州区',\n",
       "   'telephone': '(010)61569200',\n",
       "   'detail': 1,\n",
       "   'uid': 'b3f6f9b15dbf37754c6d46e2',\n",
       "   'detail_info': {'tag': '旅游景点;公园',\n",
       "    'navi_location': {'lng': 116.74665617499, 'lat': 39.881968511162},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=b3f6f9b15dbf37754c6d46e2&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '4.4',\n",
       "    'comment_num': '500',\n",
       "    'children': []}},\n",
       "  {'name': '海淀公园',\n",
       "   'location': {'lat': 39.99242, 'lng': 116.302269},\n",
       "   'address': '北京市海淀区新建宫门路2号',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '海淀区',\n",
       "   'street_id': '92df8c20d3c448cf52e5c5f5',\n",
       "   'telephone': '(010)62850569',\n",
       "   'detail': 1,\n",
       "   'uid': '92df8c20d3c448cf52e5c5f5',\n",
       "   'detail_info': {'tag': '旅游景点;公园',\n",
       "    'navi_location': {'lng': 116.30585759746, 'lat': 39.994127358518},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=92df8c20d3c448cf52e5c5f5&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '4.0',\n",
       "    'comment_num': '377',\n",
       "    'children': []}},\n",
       "  {'name': '月坛公园',\n",
       "   'location': {'lat': 39.922132, 'lng': 116.358874},\n",
       "   'address': '北京市西城区月坛北街6号',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '西城区',\n",
       "   'street_id': '06881d62cb48756c7069d3f4',\n",
       "   'telephone': '(010)68020940',\n",
       "   'detail': 1,\n",
       "   'uid': '06881d62cb48756c7069d3f4',\n",
       "   'detail_info': {'tag': '旅游景点;文物古迹',\n",
       "    'navi_location': {'lng': 116.35891118402, 'lat': 39.923993975523},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=06881d62cb48756c7069d3f4&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '4.0',\n",
       "    'comment_num': '406',\n",
       "    'children': []}},\n",
       "  {'name': '龙潭公园',\n",
       "   'location': {'lat': 39.884561, 'lng': 116.447239},\n",
       "   'address': '北京市东城区龙潭路8号',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '东城区',\n",
       "   'street_id': '090059e354c3dd9114b54b97',\n",
       "   'telephone': '(010)67144336',\n",
       "   'detail': 1,\n",
       "   'uid': '090059e354c3dd9114b54b97',\n",
       "   'detail_info': {'tag': '旅游景点;风景区',\n",
       "    'navi_location': {'lng': 116.44709723227, 'lat': 39.887279611974},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=090059e354c3dd9114b54b97&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '4.3',\n",
       "    'comment_num': '500',\n",
       "    'children': []}},\n",
       "  {'name': '日坛公园',\n",
       "   'location': {'lat': 39.921342, 'lng': 116.450437},\n",
       "   'address': '北京朝阳门外日坛北路6号',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '朝阳区',\n",
       "   'street_id': '3dc9befd7c6f810fbea240fe',\n",
       "   'telephone': '(010)85616301',\n",
       "   'detail': 1,\n",
       "   'uid': '3dc9befd7c6f810fbea240fe',\n",
       "   'detail_info': {'tag': '旅游景点;风景区',\n",
       "    'navi_location': {'lng': 116.44782392999, 'lat': 39.921275984907},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=3dc9befd7c6f810fbea240fe&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '4.4',\n",
       "    'comment_num': '500',\n",
       "    'children': []}},\n",
       "  {'name': '北海公园',\n",
       "   'location': {'lat': 39.934556, 'lng': 116.395167},\n",
       "   'address': '北京市西城区文津街1号',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '西城区',\n",
       "   'street_id': '49d959381b59a043c61e5102',\n",
       "   'telephone': '(010)64040610',\n",
       "   'detail': 1,\n",
       "   'uid': '49d959381b59a043c61e5102',\n",
       "   'detail_info': {'tag': '旅游景点;风景区',\n",
       "    'navi_location': {'lng': 116.39622434792, 'lat': 39.928644063374},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=49d959381b59a043c61e5102&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '4.3',\n",
       "    'comment_num': '408',\n",
       "    'children': []}},\n",
       "  {'name': '玉渊潭公园',\n",
       "   'location': {'lat': 39.922467, 'lng': 116.326526},\n",
       "   'address': '北京市海淀区西三环中路10号',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '海淀区',\n",
       "   'street_id': '3810c87916332c8b9d09f7b2',\n",
       "   'telephone': '(010)88653800',\n",
       "   'detail': 1,\n",
       "   'uid': '3810c87916332c8b9d09f7b2',\n",
       "   'detail_info': {'tag': '旅游景点;风景区',\n",
       "    'navi_location': {'lng': 116.31710474177, 'lat': 39.923671634717},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=3810c87916332c8b9d09f7b2&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '3.2',\n",
       "    'comment_num': '326',\n",
       "    'children': []}},\n",
       "  {'name': '念坛公园',\n",
       "   'location': {'lat': 39.711304, 'lng': 116.319487},\n",
       "   'address': '新源大街(近义和庄地铁站)',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '大兴区',\n",
       "   'street_id': 'de134f5dc2c5a2fb82288aca',\n",
       "   'telephone': '(010)61210476',\n",
       "   'detail': 1,\n",
       "   'uid': 'de134f5dc2c5a2fb82288aca',\n",
       "   'detail_info': {'tag': '旅游景点;公园',\n",
       "    'navi_location': {'lng': 116.32753558598, 'lat': 39.71371448522},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=de134f5dc2c5a2fb82288aca&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '4.4',\n",
       "    'comment_num': '190',\n",
       "    'children': []}},\n",
       "  {'name': '陶然亭公园',\n",
       "   'location': {'lat': 39.88066, 'lng': 116.388442},\n",
       "   'address': '北京市西城区太平街19号',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '西城区',\n",
       "   'street_id': 'fa01e9378b06f14a774ff1f0',\n",
       "   'telephone': '(010)63511596',\n",
       "   'detail': 1,\n",
       "   'uid': 'fa01e9378b06f14a774ff1f0',\n",
       "   'detail_info': {'tag': '旅游景点;风景区',\n",
       "    'navi_location': {'lng': 116.39381642113, 'lat': 39.882520032911},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=fa01e9378b06f14a774ff1f0&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '4.4',\n",
       "    'comment_num': '469',\n",
       "    'children': []}},\n",
       "  {'name': '奥林匹克森林公园',\n",
       "   'location': {'lat': 40.027453, 'lng': 116.398516},\n",
       "   'address': '北京市朝阳区科荟路33号',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '朝阳区',\n",
       "   'street_id': '03d7e5971f3675483c9a5e9e',\n",
       "   'telephone': '(010)64529090',\n",
       "   'detail': 1,\n",
       "   'uid': '03d7e5971f3675483c9a5e9e',\n",
       "   'detail_info': {'tag': '旅游景点;风景区',\n",
       "    'navi_location': {'lng': 116.40813941651, 'lat': 40.02738743706},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=03d7e5971f3675483c9a5e9e&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '4.3',\n",
       "    'comment_num': '500',\n",
       "    'children': []}},\n",
       "  {'name': '莲花池公园',\n",
       "   'location': {'lat': 39.898299, 'lng': 116.319518},\n",
       "   'address': '北京市丰台区西三环南路38号',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '丰台区',\n",
       "   'street_id': 'edbd1d7e2b8a8760471a3244',\n",
       "   'telephone': '(010)63982357,(010)63972892',\n",
       "   'detail': 1,\n",
       "   'uid': 'edbd1d7e2b8a8760471a3244',\n",
       "   'detail_info': {'tag': '旅游景点;公园',\n",
       "    'navi_location': {'lng': 116.3172262331, 'lat': 39.897917698131},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=edbd1d7e2b8a8760471a3244&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '4.1',\n",
       "    'comment_num': '500',\n",
       "    'children': []}},\n",
       "  {'name': '紫竹院公园',\n",
       "   'location': {'lat': 39.948064, 'lng': 116.325818},\n",
       "   'address': '北京市海淀区中关村南大街35号',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '海淀区',\n",
       "   'street_id': '49c6ce91c412a12c8d772700',\n",
       "   'telephone': '(010)68425851',\n",
       "   'detail': 1,\n",
       "   'uid': '49c6ce91c412a12c8d772700',\n",
       "   'detail_info': {'tag': '旅游景点;风景区',\n",
       "    'navi_location': {'lng': 116.32202091899, 'lat': 39.946392377119},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=49c6ce91c412a12c8d772700&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '4.5',\n",
       "    'comment_num': '426',\n",
       "    'children': []}},\n",
       "  {'name': '梨园主题公园',\n",
       "   'location': {'lat': 39.880777, 'lng': 116.684007},\n",
       "   'address': '北京市通州区梨园镇(临河里地铁站南侧)',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '通州区',\n",
       "   'street_id': '175603966ba67aa9b4265515',\n",
       "   'detail': 1,\n",
       "   'uid': '175603966ba67aa9b4265515',\n",
       "   'detail_info': {'tag': '旅游景点;公园',\n",
       "    'navi_location': {'lng': 116.68744100763, 'lat': 39.879336978819},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=175603966ba67aa9b4265515&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '3.1',\n",
       "    'comment_num': '50',\n",
       "    'children': []}},\n",
       "  {'name': '北宫国家森林公园',\n",
       "   'location': {'lat': 39.871839, 'lng': 116.12471},\n",
       "   'address': '北京市丰台区长辛店镇大灰厂东路55号',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '丰台区',\n",
       "   'street_id': 'f60a15ed8f5278ecbb17e704',\n",
       "   'telephone': '(010)83840830',\n",
       "   'detail': 1,\n",
       "   'uid': 'f60a15ed8f5278ecbb17e704',\n",
       "   'detail_info': {'tag': '旅游景点;风景区',\n",
       "    'navi_location': {'lng': 116.129089, 'lat': 39.865599080839},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=f60a15ed8f5278ecbb17e704&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '3.7',\n",
       "    'comment_num': '114',\n",
       "    'children': []}},\n",
       "  {'name': '北京动物园',\n",
       "   'location': {'lat': 39.947735, 'lng': 116.343376},\n",
       "   'address': '北京市西城区西直门外大街137号',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '西城区',\n",
       "   'street_id': '4bb049d91598944799005000',\n",
       "   'telephone': '(010)68314411,(010)68390274',\n",
       "   'detail': 1,\n",
       "   'uid': '4bb049d91598944799005000',\n",
       "   'detail_info': {'tag': '旅游景点;动物园',\n",
       "    'navi_location': {'lng': 116.34531849036, 'lat': 39.944191992334},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=4bb049d91598944799005000&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '4.1',\n",
       "    'comment_num': '356',\n",
       "    'children': []}},\n",
       "  {'name': '北京野生动物园',\n",
       "   'location': {'lat': 39.502531, 'lng': 116.342213},\n",
       "   'address': '北京市大兴区榆垡镇万亩森林内',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '大兴区',\n",
       "   'street_id': 'e85e2527b6f7615395f08cf8',\n",
       "   'telephone': '(010)89216606',\n",
       "   'detail': 1,\n",
       "   'uid': 'e85e2527b6f7615395f08cf8',\n",
       "   'detail_info': {'tag': '旅游景点;动物园',\n",
       "    'navi_location': {'lng': 116.32936938595, 'lat': 39.503047664253},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=e85e2527b6f7615395f08cf8&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '4.1',\n",
       "    'comment_num': '124',\n",
       "    'children': []}},\n",
       "  {'name': '北京植物园',\n",
       "   'location': {'lat': 40.007561, 'lng': 116.216186},\n",
       "   'address': '北京市海淀区香山南路',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '海淀区',\n",
       "   'street_id': '550fd5f9c8d53478931fc9c4',\n",
       "   'telephone': '(010)82598771',\n",
       "   'detail': 1,\n",
       "   'uid': '550fd5f9c8d53478931fc9c4',\n",
       "   'detail_info': {'tag': '旅游景点;植物园',\n",
       "    'navi_location': {'lng': 116.216342, 'lat': 39.999807069994},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=550fd5f9c8d53478931fc9c4&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '4.0',\n",
       "    'comment_num': '157',\n",
       "    'children': []}},\n",
       "  {'name': '北京西山国家森林公园(昌华园区)',\n",
       "   'location': {'lat': 39.978428, 'lng': 116.209339},\n",
       "   'address': '北京市海淀区闵庄路与香山南路交叉口西行100米',\n",
       "   'province': '北京市',\n",
       "   'city': '北京市',\n",
       "   'area': '海淀区',\n",
       "   'street_id': '1d4bc84384a3c926eedfca70',\n",
       "   'telephone': '(010)62720251',\n",
       "   'detail': 1,\n",
       "   'uid': '1d4bc84384a3c926eedfca70',\n",
       "   'detail_info': {'tag': '旅游景点;公园',\n",
       "    'navi_location': {'lng': 116.21123524682, 'lat': 39.978974404551},\n",
       "    'type': 'scope',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=1d4bc84384a3c926eedfca70&output=html&source=placeapi_v2',\n",
       "    'overall_rating': '4.3',\n",
       "    'comment_num': '500',\n",
       "    'children': []}}]}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#!/usr/bin/python\n",
    "# coding: utf-8\n",
    "\n",
    "import requests\n",
    "import json\n",
    "\n",
    "def getjson(loc):\n",
    "    headers = {'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} \n",
    "    pa = {\n",
    "        'q': '公园', \n",
    "        'region': loc,\n",
    "        'scope': '2',\n",
    "        'page_size': 20,\n",
    "        'page_num': 0, \n",
    "        'output': 'json',\n",
    "        'ak': 'DDtVK6HPruSSkqHRj5gTk0rc'\n",
    "    }\n",
    "    r = requests.get(\"http://api.map.baidu.com/place/v2/search\", params=pa, headers= headers)\n",
    "    decodejson = json.loads(r.text)\n",
    "    return decodejson\n",
    "\n",
    "getjson('北京市')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 14.3.1 获取所有拥有公园的城市"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-13T15:32:37.164584Z",
     "start_time": "2018-12-13T15:32:36.533250Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "苏州市 3067\n",
      "南京市 2547\n",
      "无锡市 2324\n",
      "常州市 1432\n",
      "南通市 1201\n",
      "徐州市 1057\n",
      "盐城市 850\n",
      "宿迁市 818\n",
      "扬州市 609\n",
      "镇江市 568\n",
      "泰州市 558\n",
      "淮安市 520\n",
      "连云港市 427\n",
      "杭州市 2495\n",
      "温州市 2114\n",
      "宁波市 1764\n",
      "嘉兴市 869\n",
      "金华市 776\n",
      "台州市 661\n",
      "绍兴市 632\n",
      "湖州市 479\n",
      "丽水市 379\n",
      "衢州市 192\n",
      "舟山市 131\n",
      "广州市 4366\n",
      "深圳市 4850\n",
      "东莞市 2493\n",
      "佛山市 2652\n",
      "惠州市 1386\n",
      "江门市 776\n",
      "中山市 729\n",
      "珠海市 478\n",
      "汕头市 452\n",
      "梅州市 397\n",
      "湛江市 379\n",
      "韶关市 367\n",
      "河源市 350\n",
      "肇庆市 342\n",
      "清远市 303\n",
      "茂名市 295\n",
      "揭阳市 226\n",
      "阳江市 189\n",
      "汕尾市 177\n",
      "潮州市 155\n",
      "云浮市 87\n",
      "福州市 2217\n",
      "厦门市 1364\n",
      "泉州市 1104\n",
      "漳州市 621\n",
      "莆田市 353\n",
      "宁德市 312\n",
      "龙岩市 304\n",
      "南平市 253\n",
      "三明市 245\n",
      "青岛市 1750\n",
      "济南市 976\n",
      "潍坊市 841\n",
      "烟台市 735\n",
      "威海市 680\n",
      "淄博市 528\n",
      "临沂市 525\n",
      "济宁市 447\n",
      "日照市 401\n",
      "菏泽市 361\n",
      "聊城市 330\n",
      "德州市 283\n",
      "滨州市 268\n",
      "枣庄市 264\n",
      "泰安市 251\n",
      "东营市 211\n",
      "莱芜市 116\n",
      "郑州市 2061\n",
      "洛阳市 784\n",
      "开封市 506\n",
      "新乡市 447\n",
      "南阳市 411\n",
      "商丘市 337\n",
      "焦作市 312\n",
      "平顶山市 269\n",
      "安阳市 263\n",
      "周口市 230\n",
      "三门峡市 215\n",
      "信阳市 211\n",
      "许昌市 194\n",
      "驻马店市 183\n",
      "濮阳市 134\n",
      "漯河市 121\n",
      "鹤壁市 103\n",
      "济源市 30\n",
      "石家庄市 1342\n",
      "保定市 777\n",
      "邢台市 633\n",
      "秦皇岛市 630\n",
      "唐山市 606\n",
      "邯郸市 546\n",
      "沧州市 366\n",
      "廊坊市 357\n",
      "张家口市 345\n",
      "承德市 269\n",
      "衡水市 171\n",
      "成都市 3436\n",
      "绵阳市 462\n",
      "南充市 447\n",
      "德阳市 315\n",
      "内江市 270\n",
      "遂宁市 242\n",
      "宜宾市 202\n",
      "乐山市 190\n",
      "泸州市 182\n",
      "眉山市 169\n",
      "资阳市 167\n",
      "达州市 154\n",
      "自贡市 142\n",
      "广元市 140\n",
      "攀枝花市 128\n",
      "广安市 113\n",
      "凉山彝族自治州 98\n",
      "巴中市 78\n",
      "雅安市 78\n",
      "阿坝藏族羌族自治州 58\n",
      "甘孜藏族自治州 55\n",
      "沈阳市 1410\n",
      "大连市 1625\n",
      "抚顺市 380\n",
      "鞍山市 274\n",
      "营口市 211\n",
      "盘锦市 203\n",
      "葫芦岛市 171\n",
      "本溪市 159\n",
      "锦州市 156\n",
      "丹东市 153\n",
      "阜新市 109\n",
      "朝阳市 96\n",
      "铁岭市 92\n",
      "辽阳市 89\n",
      "昆明市 1756\n",
      "大理白族自治州 356\n",
      "玉溪市 285\n",
      "西双版纳傣族自治州 275\n",
      "曲靖市 226\n",
      "丽江市 164\n",
      "红河哈尼族彝族自治州 142\n",
      "普洱市 115\n",
      "迪庆藏族自治州 110\n",
      "楚雄彝族自治州 109\n",
      "昭通市 106\n",
      "保山市 102\n",
      "临沧市 92\n",
      "文山壮族苗族自治州 67\n",
      "德宏傣族景颇族自治州 51\n",
      "怒江傈僳族自治州 14\n",
      "长沙市 1831\n",
      "张家界市 463\n",
      "郴州市 445\n",
      "株洲市 421\n",
      "常德市 411\n",
      "衡阳市 350\n",
      "岳阳市 247\n",
      "邵阳市 219\n",
      "永州市 196\n",
      "娄底市 181\n",
      "湘潭市 144\n",
      "怀化市 138\n",
      "益阳市 133\n",
      "湘西土家族苗族自治州 70\n",
      "武汉市 2856\n",
      "黄冈市 922\n",
      "宜昌市 395\n",
      "荆州市 293\n",
      "十堰市 283\n",
      "荆门市 281\n",
      "襄阳市 262\n",
      "黄石市 236\n",
      "孝感市 202\n",
      "咸宁市 184\n",
      "随州市 134\n",
      "恩施土家族苗族自治州 117\n",
      "神农架林区 78\n",
      "仙桃市 57\n",
      "鄂州市 50\n",
      "潜江市 46\n",
      "天门市 41\n",
      "南昌市 858\n",
      "赣州市 717\n",
      "九江市 469\n",
      "新余市 462\n",
      "上饶市 365\n",
      "萍乡市 301\n",
      "宜春市 284\n",
      "吉安市 262\n",
      "抚州市 239\n",
      "景德镇市 137\n",
      "鹰潭市 78\n",
      "合肥市 1349\n",
      "安庆市 431\n",
      "宿州市 323\n",
      "阜阳市 312\n",
      "芜湖市 302\n",
      "六安市 283\n",
      "滁州市 206\n",
      "亳州市 195\n",
      "淮南市 180\n",
      "宣城市 170\n",
      "马鞍山市 164\n",
      "蚌埠市 146\n",
      "黄山市 138\n",
      "巢湖市 132\n",
      "淮北市 113\n",
      "池州市 83\n",
      "铜陵市 77\n",
      "太原市 811\n",
      "临汾市 345\n",
      "长治市 294\n",
      "运城市 289\n",
      "晋中市 267\n",
      "大同市 249\n",
      "晋城市 189\n",
      "吕梁市 166\n",
      "忻州市 159\n",
      "阳泉市 105\n",
      "朔州市 79\n",
      "南宁市 1102\n",
      "桂林市 666\n",
      "柳州市 588\n",
      "玉林市 410\n",
      "北海市 288\n",
      "百色市 191\n",
      "河池市 183\n",
      "梧州市 150\n",
      "贵港市 142\n",
      "钦州市 91\n",
      "防城港市 78\n",
      "来宾市 72\n",
      "贺州市 68\n",
      "崇左市 46\n",
      "西安市 3222\n",
      "宝鸡市 469\n",
      "渭南市 330\n",
      "咸阳市 251\n",
      "汉中市 183\n",
      "榆林市 173\n",
      "商洛市 156\n",
      "安康市 125\n",
      "延安市 95\n",
      "铜川市 83\n",
      "哈尔滨市 1228\n",
      "齐齐哈尔市 178\n",
      "牡丹江市 178\n",
      "佳木斯市 169\n",
      "大庆市 161\n",
      "黑河市 137\n",
      "伊春市 134\n",
      "鹤岗市 97\n",
      "绥化市 78\n",
      "鸡西市 74\n",
      "双鸭山市 56\n",
      "七台河市 37\n",
      "大兴安岭地区 17\n",
      "呼和浩特市 518\n",
      "包头市 435\n",
      "鄂尔多斯市 392\n",
      "赤峰市 203\n",
      "呼伦贝尔市 168\n",
      "乌海市 163\n",
      "乌兰察布市 125\n",
      "通辽市 122\n",
      "巴彦淖尔市 117\n",
      "阿拉善盟 101\n",
      "兴安盟 101\n",
      "锡林郭勒盟 82\n",
      "贵阳市 1491\n",
      "遵义市 384\n",
      "毕节地区 202\n",
      "黔南布依族苗族自治州 193\n",
      "六盘水市 191\n",
      "黔东南苗族侗族自治州 147\n",
      "铜仁地区 141\n",
      "黔西南布依族苗族自治州 106\n",
      "安顺市 104\n",
      "长春市 1404\n",
      "延边朝鲜族自治州 431\n",
      "吉林市 269\n",
      "四平市 186\n",
      "通化市 102\n",
      "松原市 98\n",
      "白山市 95\n",
      "白城市 78\n",
      "辽源市 44\n",
      "兰州市 484\n",
      "白银市 252\n",
      "金昌市 181\n",
      "平凉市 167\n",
      "酒泉市 160\n",
      "张掖市 128\n",
      "武威市 128\n",
      "定西市 111\n",
      "天水市 98\n",
      "临夏回族自治州 61\n",
      "陇南市 56\n",
      "庆阳市 48\n",
      "甘南藏族自治州 42\n",
      "嘉峪关市 22\n",
      "乌鲁木齐市 520\n",
      "昌吉回族自治州 165\n",
      "伊犁哈萨克自治州 160\n",
      "喀什地区 91\n",
      "克拉玛依市 79\n",
      "阿勒泰地区 75\n",
      "塔城地区 64\n",
      "阿克苏地区 58\n",
      "巴音郭楞蒙古自治州 50\n",
      "哈密地区 38\n",
      "吐鲁番地区 25\n",
      "石河子市 23\n",
      "和田地区 22\n",
      "博尔塔拉蒙古自治州 20\n",
      "克孜勒苏柯尔克孜自治州 9\n",
      "图木舒克市 6\n",
      "五家渠市 5\n",
      "阿拉尔市 3\n",
      "海口市 430\n",
      "三亚市 257\n",
      "文昌市 49\n",
      "儋州市 36\n",
      "临高县 33\n",
      "定安县 25\n",
      "澄迈县 24\n",
      "昌江黎族自治县 20\n",
      "万宁市 19\n",
      "东方市 14\n",
      "琼海市 14\n",
      "保亭黎族苗族自治县 12\n",
      "陵水黎族自治县 12\n",
      "乐东黎族自治县 11\n",
      "琼中黎族苗族自治县 9\n",
      "屯昌县 8\n",
      "五指山市 5\n",
      "白沙黎族自治县 3\n",
      "银川市 673\n",
      "固原市 75\n",
      "石嘴山市 71\n",
      "吴忠市 64\n",
      "中卫市 34\n",
      "西宁市 274\n",
      "海西蒙古族藏族自治州 60\n",
      "海南藏族自治州 35\n",
      "海东地区 34\n",
      "海北藏族自治州 16\n",
      "黄南藏族自治州 10\n",
      "玉树藏族自治州 7\n",
      "果洛藏族自治州 5\n",
      "拉萨市 56\n",
      "阿里地区 19\n",
      "林芝地区 15\n",
      "日喀则地区 14\n",
      "山南地区 5\n",
      "昌都地区 4\n",
      "那曲地区 2\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/python\n",
    "# coding: utf-8\n",
    "\n",
    "import requests\n",
    "import json\n",
    "\n",
    "def getjson(loc):\n",
    "    headers = {'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} \n",
    "    pa = {\n",
    "        'q': '公园', \n",
    "        'region': loc,\n",
    "        'scope': '2',\n",
    "        'page_size': 20,\n",
    "        'page_num': 0, \n",
    "        'output': 'json',\n",
    "        'ak': 'DDtVK6HPruSSkqHRj5gTk0rc'\n",
    "    }\n",
    "    r = requests.get(\"http://api.map.baidu.com/place/v2/search\", params=pa, headers= headers)\n",
    "    decodejson = json.loads(r.text)\n",
    "    return decodejson\n",
    "\n",
    "province_list = ['江苏省', '浙江省', '广东省', '福建省', '山东省', '河南省', '河北省', '四川省', '辽宁省', '云南省', \n",
    "                 '湖南省', '湖北省', '江西省', '安徽省', '山西省', '广西壮族自治区', '陕西省', '黑龙江省', '内蒙古自治区', \n",
    "                 '贵州省', '吉林省', '甘肃省', '新疆维吾尔自治区', '海南省', '宁夏回族自治区', '青海省', '西藏自治区']\n",
    "for eachprovince in province_list:\n",
    "    decodejson = getjson(eachprovince)\n",
    "    for eachcity in decodejson['results']:\n",
    "        city = eachcity['name']\n",
    "        num = eachcity['num']\n",
    "        print (city, num)\n",
    "        output = '\\t'.join([city, str(num)]) + '\\r\\n'\n",
    "        with open('cities.txt', \"a+\", encoding='utf-8') as f:\n",
    "            f.write(output)\n",
    "            f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-13T15:36:28.375882Z",
     "start_time": "2018-12-13T15:36:28.176502Z"
    }
   },
   "outputs": [],
   "source": [
    "#!/usr/bin/python\n",
    "# coding: utf-8\n",
    "\n",
    "import requests\n",
    "import json\n",
    "def getjson(loc):\n",
    "    headers = {'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} \n",
    "    pa = {\n",
    "        'q': '公园', \n",
    "        'region': loc,\n",
    "        'scope': '2',\n",
    "        'page_size': 20,\n",
    "        'page_num': 0, \n",
    "        'output': 'json',\n",
    "        'ak': 'DDtVK6HPruSSkqHRj5gTk0rc'\n",
    "    }\n",
    "    r = requests.get(\"http://api.map.baidu.com/place/v2/search\", params=pa, headers= headers)\n",
    "    decodejson = json.loads(r.text)\n",
    "    return decodejson\n",
    "\n",
    "decodejson = getjson('全国')\n",
    "six_cities_list = ['北京市','上海市','重庆市','天津市','香港特别行政区','澳门特别行政区',]\n",
    "for eachprovince in decodejson['results']:\n",
    "    city = eachprovince['name']\n",
    "    num = eachprovince['num']\n",
    "    if city in six_cities_list:\n",
    "        output = '\\t'.join([city, str(num)]) + '\\r\\n'\n",
    "        with open('cities.txt', \"a+\", encoding='utf-8') as f:\n",
    "            f.write(output)\n",
    "            f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 14.3.2 获取所有城市的公园数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-13T15:55:14.821334Z",
     "start_time": "2018-12-13T15:55:14.761426Z"
    }
   },
   "outputs": [],
   "source": [
    "#coding=utf-8\n",
    "import pymysql\n",
    "\n",
    "db = pymysql.connect(\"localhost\",\"root\",\"password\",\"baidumap\")\n",
    "cursor = db.cursor()\n",
    "\n",
    "sql = \"\"\"CREATE TABLE city (\n",
    "         id INT NOT NULL AUTO_INCREMENT,\n",
    "         city VARCHAR(200) NOT NULL,\n",
    "         park VARCHAR(200) NOT NULL,\n",
    "         location_lat FLOAT,\n",
    "         location_lng FLOAT,\n",
    "         address VARCHAR(200),\n",
    "         street_id VARCHAR(200),\n",
    "         uid VARCHAR(200),\n",
    "         created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n",
    "         PRIMARY KEY (id)\n",
    "         );\"\"\"\n",
    "cursor.execute(sql)\n",
    "db.commit()\n",
    "db.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-13T15:55:57.540102Z",
     "start_time": "2018-12-13T15:55:57.536061Z"
    }
   },
   "outputs": [],
   "source": [
    "city_list = list()\n",
    "with open(\"cities.txt\", 'r', encoding='utf-8') as txt_file:   \n",
    "    for eachLine in txt_file:\n",
    "        if eachLine != \"\" and eachLine != \"\\n\":\n",
    "            fields = eachLine.split(\"\\t\")\n",
    "            city = fields[0]\n",
    "            city_list.append(city)\n",
    "    txt_file.close()   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-13T15:58:43.104349Z",
     "start_time": "2018-12-13T15:58:24.590124Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "苏州市 0\n",
      "苏州市 1\n",
      "苏州市 2\n",
      "苏州市 3\n",
      "苏州市 4\n",
      "苏州市 5\n",
      "苏州市 6\n",
      "苏州市 7\n",
      "苏州市 8\n",
      "苏州市 9\n",
      "苏州市 10\n",
      "苏州市 11\n",
      "苏州市 12\n",
      "苏州市 13\n",
      "苏州市 14\n",
      "苏州市 15\n",
      "苏州市 16\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'results'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-16-a8ee60ff83f2>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     32\u001b[0m         \u001b[0mdecodejson\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgetjson\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0meachcity\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpage_num\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     33\u001b[0m         \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0meachcity\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpage_num\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 34\u001b[1;33m         \u001b[1;32mif\u001b[0m \u001b[0mdecodejson\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'results'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     35\u001b[0m             \u001b[1;32mfor\u001b[0m \u001b[0meachone\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mdecodejson\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'results'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     36\u001b[0m                 \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'results'"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/python\n",
    "# coding: utf-8\n",
    "\n",
    "import requests\n",
    "import json\n",
    "import pymysql\n",
    "import time\n",
    "\n",
    "db = pymysql.connect(\"localhost\",\"root\",\"password\",\"baidumap\")\n",
    "cursor = db.cursor()\n",
    "\n",
    "def getjson(loc,page_num):\n",
    "    headers = {'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} \n",
    "    pa = {\n",
    "        'q': '公园', \n",
    "        'region': loc,\n",
    "        'scope': '2',\n",
    "        'page_size': 20,\n",
    "        'page_num': page_num, \n",
    "        'output': 'json',\n",
    "        'ak': 'DDtVK6HPruSSkqHRj5gTk0rc'\n",
    "    }\n",
    "    r = requests.get(\"http://api.map.baidu.com/place/v2/search\", params=pa, headers= headers)\n",
    "    decodejson = json.loads(r.text)\n",
    "    time.sleep(1)\n",
    "    return decodejson\n",
    "\n",
    "for eachcity in city_list:\n",
    "    not_last_page = True\n",
    "    page_num = 0\n",
    "    while not_last_page:\n",
    "        decodejson = getjson(eachcity, page_num)\n",
    "        print (eachcity, page_num)\n",
    "        if decodejson['results']: \n",
    "            for eachone in decodejson['results']:\n",
    "                try:\n",
    "                    park = eachone['name']\n",
    "                except:\n",
    "                    park = None\n",
    "                try:\n",
    "                    location_lat = eachone['location']['lat']\n",
    "                except:\n",
    "                    location_lat = None\n",
    "                try:\n",
    "                    location_lng = eachone['location']['lng']\n",
    "                except:\n",
    "                    location_lng = None            \n",
    "                try:\n",
    "                    address = eachone['address']\n",
    "                except:\n",
    "                    address = None              \n",
    "                try:\n",
    "                    street_id = eachone['street_id']\n",
    "                except:\n",
    "                    street_id = None            \n",
    "                try:\n",
    "                    uid = eachone['uid']\n",
    "                except:\n",
    "                    uid = None\n",
    "                sql = \"\"\"INSERT INTO baidumap.city\n",
    "                (city, park, location_lat, location_lng, address, street_id, uid)\n",
    "                VALUES\n",
    "                (%s, %s, %s, %s, %s, %s, %s);\"\"\"\n",
    "    \n",
    "                cursor.execute(sql, (eachcity, park, location_lat, location_lng, address, street_id, uid,))\n",
    "                db.commit()\n",
    "            page_num += 1\n",
    "        else:        \n",
    "            not_last_page = False  \n",
    "cursor.close()\n",
    "db.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 14.3.3 获取所有公园的详细信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-13T16:01:34.483155Z",
     "start_time": "2018-12-13T16:01:34.385117Z"
    }
   },
   "outputs": [],
   "source": [
    "#coding=utf-8\n",
    "import pymysql\n",
    "\n",
    "db = pymysql.connect(\"localhost\",\"root\",\"password\",\"baidumap\")\n",
    "cursor = db.cursor()\n",
    "sql = \"\"\"CREATE TABLE park (\n",
    "         id INT NOT NULL AUTO_INCREMENT,\n",
    "         park VARCHAR(200) NOT NULL,\n",
    "         location_lat FLOAT,\n",
    "         location_lng FLOAT,\n",
    "         address VARCHAR(200),\n",
    "         street_id VARCHAR(200),\n",
    "         telephone VARCHAR(200),\n",
    "         detail INT,\n",
    "         uid VARCHAR(200),\n",
    "         tag VARCHAR(200),\n",
    "         type VARCHAR(200),\n",
    "         detail_url VARCHAR(800),\n",
    "         price INT,\n",
    "         overall_rating FLOAT,\n",
    "         image_num INT,\n",
    "         comment_num INT,\n",
    "         shop_hours VARCHAR(800),\n",
    "         alias VARCHAR(800),\n",
    "         keyword VARCHAR(800),\n",
    "         scope_type VARCHAR(200),\n",
    "         scope_grade VARCHAR(200),\n",
    "         description VARCHAR(9000),\n",
    "         created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n",
    "         PRIMARY KEY (id)\n",
    "         );\"\"\"\n",
    "cursor.execute(sql)\n",
    "db.commit()\n",
    "db.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-13T16:02:48.076244Z",
     "start_time": "2018-12-13T16:02:48.061202Z"
    }
   },
   "outputs": [],
   "source": [
    "#!/usr/bin/python\n",
    "# coding: utf-8\n",
    "\n",
    "import requests\n",
    "import json\n",
    "import pymysql\n",
    "\n",
    "db = pymysql.connect(\"localhost\",\"root\",\"password\",\"baidumap\")\n",
    "cursor = db.cursor()\n",
    "sql = \"Select uid from baidumap.city where id > 0;\"\n",
    "\n",
    "cursor.execute(sql)\n",
    "db.commit()\n",
    "results = cursor.fetchall()\n",
    "db.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-13T16:06:17.134717Z",
     "start_time": "2018-12-13T16:05:56.629816Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "764672e507ca6fb495ec849f\n",
      "56f2f696559dd9800aa8d441\n",
      "32e3ea700d452dac4d554706\n",
      "9e99a96fdd6865de081cc7a6\n",
      "083a55faa1349d60256e98b9\n",
      "5b25f4468c4cb2ea1029354c\n",
      "f0e0156bea280b773a04b1aa\n",
      "9d3e819abd3ab36a1d1d11ec\n",
      "c5ddcc087a310fb8d813180e\n",
      "4201e6c702802a026d47603f\n",
      "0104e62ad2d314fc51e5c526\n",
      "d305db1fd784d03e932ca350\n",
      "2cc3c8e85dfe8ed64d2fdfd7\n",
      "c7474618595979fbcb213de3\n",
      "3b7e2c39a87633309de32192\n",
      "beabdbf449b169987069d341\n",
      "e30828481d50f1c1b34c4914\n",
      "e56bf5e32cfb51440782c24f\n",
      "fa7da3e8b849db6242d63e4f\n",
      "d4034eb3ce56638bc036447a\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'result'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-23-8b96b41a12a0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     29\u001b[0m     \u001b[0mdecodejson\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgetjson\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0muid\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     30\u001b[0m     \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0muid\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 31\u001b[1;33m     \u001b[0minfo\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdecodejson\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'result'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     32\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     33\u001b[0m         \u001b[0mpark\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minfo\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'name'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'result'"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "import pymysql\n",
    "import time\n",
    "\n",
    "db = pymysql.connect(\"localhost\",\"root\",\"password\",\"baidumap\")\n",
    "cursor = db.cursor()\n",
    "sql = \"Select uid from baidumap.city where id > 0;\"\n",
    "\n",
    "cursor.execute(sql)\n",
    "db.commit()\n",
    "results = cursor.fetchall()\n",
    "\n",
    "def getjson(uid):\n",
    "    headers = {'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} \n",
    "    pa = {\n",
    "        'uid': uid, \n",
    "        'scope': '2',\n",
    "        'output': 'json',\n",
    "        'ak': 'DDtVK6HPruSSkqHRj5gTk0rc'\n",
    "    }\n",
    "    r = requests.get(\"http://api.map.baidu.com/place/v2/detail\", params=pa, headers= headers)\n",
    "    time.sleep(1)\n",
    "    decodejson = json.loads(r.text)\n",
    "    return decodejson\n",
    "\n",
    "for row in results:\n",
    "    uid = row[0]\n",
    "    decodejson = getjson(uid)\n",
    "    print (uid)\n",
    "    info = decodejson['result']\n",
    "    try:\n",
    "        park = info['name']\n",
    "    except:\n",
    "        park = None\n",
    "    try:\n",
    "        location_lat = info['location']['lat']\n",
    "    except:\n",
    "        location_lat = None\n",
    "    try:\n",
    "        location_lng = info['location']['lng']\n",
    "    except:\n",
    "        location_lng = None            \n",
    "    try:\n",
    "        address = info['address']\n",
    "    except:\n",
    "        address = None              \n",
    "    try:\n",
    "        street_id = info['street_id']\n",
    "    except:\n",
    "        street_id = None\n",
    "    try:\n",
    "        telephone = info['telephone']\n",
    "    except:\n",
    "        telephone = None\n",
    "    try:\n",
    "        detail = info['detail']\n",
    "    except:\n",
    "        detail = None \n",
    "    try:\n",
    "        tag = info['detail_info']['tag']\n",
    "    except:\n",
    "        tag = None            \n",
    "    try:\n",
    "        detail_url = info['detail_info']['detail_url']\n",
    "    except:\n",
    "        detail_url = None\n",
    "    try:\n",
    "        type = info['detail_info']['type']\n",
    "    except:\n",
    "        type = None            \n",
    "    try:\n",
    "        overall_rating = info['detail_info']['overall_rating']\n",
    "    except:\n",
    "        overall_rating = None\n",
    "    try:\n",
    "        image_num = info['detail_info']['image_num']\n",
    "    except:\n",
    "        image_num = None\n",
    "    try:\n",
    "        comment_num = info['detail_info']['comment_num']\n",
    "    except:\n",
    "        comment_num = None\n",
    "    try:\n",
    "        key_words = ''\n",
    "        key_words_list = info['detail_info']['di_review_keyword']\n",
    "        for eachone in key_words_list:\n",
    "            key_words = key_words + eachone['keyword'] + '/'\n",
    "    except:\n",
    "        key_words = None\n",
    "    try:\n",
    "        shop_hours = info['detail_info']['shop_hours']\n",
    "    except:\n",
    "        shop_hours = None            \n",
    "    try:\n",
    "        alias = info['detail_info']['alias']\n",
    "    except:\n",
    "        alias = None\n",
    "    try:\n",
    "        scope_type = info['detail_info']['scope_type']\n",
    "    except:\n",
    "        scope_type = None            \n",
    "    try:\n",
    "        scope_grade = info['detail_info']['scope_grade']\n",
    "    except:\n",
    "        scope_grade = None\n",
    "    try:\n",
    "        description = info['detail_info']['description']\n",
    "    except:\n",
    "        description = None\n",
    "    sql = \"\"\"INSERT INTO baidumap.park\n",
    "    (park, location_lat, location_lng, address, street_id, uid, telephone, detail, tag, detail_url, type, overall_rating, image_num, \n",
    "    comment_num, keyword, shop_hours, alias, scope_type, scope_grade, description)\n",
    "    VALUES\n",
    "    (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);\"\"\"\n",
    "\n",
    "    cursor.execute(sql, (park, location_lat, location_lng, address, street_id, uid, telephone, detail, tag, detail_url, \n",
    "                      type, overall_rating, image_num, comment_num, key_words, shop_hours, alias, scope_type, scope_grade, description,))\n",
    "    db.commit()\n",
    "cursor.close()\n",
    "db.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
